package com.xauto.pioneer.appuser.activity.takeCar;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.xauto.pioneer.appuser.MyApplication;
import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.activity.BaseActivity;
import com.xauto.pioneer.appuser.activity.common.TakePhotoActivity;
import com.xauto.pioneer.appuser.http.BaseLHttpHandler;
import com.xauto.pioneer.appuser.http.Constants;
import com.xauto.pioneer.appuser.http.LHttpLib;
import com.xauto.pioneer.appuser.model.Image;
import com.xauto.pioneer.appuser.model.JSONStatus;
import com.xauto.pioneer.appuser.util.PreferencesUtils;
import com.xauto.pioneer.appuser.util.TimeUtil;
import com.xauto.pioneer.appuser.util.ViewUtil;
import com.xauto.pioneer.appuser.view.PromptDialog;
import com.xauto.pioneer.appuser.view.TopBar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

import static com.xauto.pioneer.appuser.activity.common.TakePhotoActivity.DO_BIG_IMAGE;
import static com.xauto.pioneer.appuser.activity.common.TakePhotoActivity.UPLOAD_IMG;

/**
 * 还车界面
 * Created by Ellis.Li on 2017/5/17.
 */

public class TakePhotoReturnCarActivity extends BaseActivity {
    @Bind(R.id.topBar)
    TopBar topBar;
    @Bind(R.id.tvIdInfoNext)
    TextView tvIdInfoNext;
    @Bind(R.id.ivIdInfo)
    ImageView ivIdInfo;
    @Bind(R.id.tvIdInfoTakePhoto)
    TextView tvIdInfoTakePhoto;
    //-------------------------
    @Bind(R.id.ivIdInfo_two)
    ImageView ivIdInfo_two;
    @Bind(R.id.tvIdInfoTakePhoto_two)
    TextView tvIdInfoTakePhoto_two;
    @Bind(R.id.ivIdInfo_three)
    ImageView ivIdInfo_three;
    @Bind(R.id.tvIdInfoTakePhoto_three)
    TextView tvIdInfoTakePhoto_three;
    @Bind(R.id.ivIdInfo_four)
    ImageView ivIdInfo_four;
    @Bind(R.id.tvIdInfoTakePhoto_four)
    TextView tvIdInfoTakePhoto_four;
    //-----------------------------
    private int onePc=0;
    private int twoPc=0;
    private int threePc=0;
    private int fourPc=0;
    private int witchPc=0;
    String Path1="";
    String Path2="";
    String Path3="";
    String Path4="";
    String localPauth1="";
    String localPauth2="";
    String localPauth3="";
    String localPauth4="";
    //@Bind(R.id.photosLinear)
    //LinearLayout photosLinear;
    //@Bind(R.id.viewPagePark)
    //ViewPager viewPagePark;
    //@Bind(R.id.tvPromptSuccess)
    //TextView tvPromptSuccess;
    //@Bind(R.id.noParkLinear)
    //LinearLayout noParkLinear;
   // @Bind(R.id.spaceView)
    //LinearLayout spaceView;
    String car_order_no = "";
    String parking_site_no = "";
    //private List<ParkSpace> parkSpaceList;
    private List<String> ImageList=new ArrayList();
    //private ViewPagerAdapter orderTotalAdapter;
    //@Bind(R.id.groupLinear)
    //LinearLayout groupLinear;
    //private ImageView[] ivPoints;//小圆点图片的集合
    /** 当前上传照片的服务器路径**/
    private String currentUpdatePath = "";
    /** 当前选择的停车位编码**/
   //private String currentSpaceNo = "";
    /**
     * viewpager 数据
     */
    //private List<View> views;
    private PromptDialog promptDialog;

    /**
     * 入口
     *
     * @param mContext
     * @param parking_site_no 网点编号
     * @param car_order_no    订单编号
     */
    public static void startAction(Activity mContext, String car_order_no, String parking_site_no, int requestCode) {
        Intent intent = new Intent(mContext, TakePhotoReturnCarActivity.class);
        intent.putExtra("car_order_no",car_order_no);
        intent.putExtra("parking_site_no",parking_site_no);
        mContext.startActivityForResult(intent,requestCode);
    }

    @Override
    protected int getContentView() {
        return R.layout.ac_return_car;
    }

    @Override
    protected void initView() {
        car_order_no = getIntent().getStringExtra("car_order_no");
        parking_site_no = getIntent().getStringExtra("parking_site_no");
        topBar.showView(TopBar.TITLE, TopBar.LEFT_IMAGE_BTN);
        topBar.setText(TopBar.TITLE, "还车");
        topBar.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
        topBar.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        //未选择图片之前 此按钮不能点击
        tvIdInfoNext.setClickable(false);

        /*views = new ArrayList<>();
        orderTotalAdapter = new ViewPagerAdapter(mContext, views);
        viewPagePark.setAdapter(orderTotalAdapter);*/
    }

    @Override
    protected void initData() {
       // loadEvent();
    }

    @OnClick({R.id.tvIdInfoNext, R.id.tvIdInfoTakePhoto,R.id.tvIdInfoTakePhoto_two,R.id.tvIdInfoTakePhoto_three,R.id.tvIdInfoTakePhoto_four,
    R.id.ivIdInfo,R.id.ivIdInfo_two,R.id.ivIdInfo_three,R.id.ivIdInfo_four,})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvIdInfoNext:
                promptDialog = new PromptDialog(mContext, getString(R.string.ensure_return_car), getString(R.string.ensure_return_car_hint),
                        getString(R.string.confirm),getString(R.string.cancel),mContext.getString(R.string.dialogpic_notice),new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        promptDialog.dismiss();
                        String memberId = PreferencesUtils.getString(mContext, Constants.KEY_MEMBER_ID);
                        //拼接信息
                        JSONObject car_order_info = new JSONObject();
                        JSONArray arry=new JSONArray();
                        arry.put(Path1);
                        arry.put(Path2);
                        arry.put(Path3);
                        arry.put(Path4);
                        try {
                            car_order_info.put("parking_site_no", parking_site_no);
                            car_order_info.put("car_order_return_images", arry);
                           // car_order_info.put("parking_space_no", currentSpaceNo);
                           // car_order_info.put("car_order_return_image", currentUpdatePath);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        new LHttpLib().applyReturnCar(mContext, memberId, car_order_no, car_order_info, new BaseLHttpHandler(mContext, true, true) {
                            @Override
                            public void onSuccess(JSONStatus jsonStatus) {
                                super.onSuccess(jsonStatus);
                                //TODO 申请还车成功
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        PreferencesUtils.putString(mContext,"Park_NO","0");
                                        FoundCarActivity.Order=0;
                                        FoundCarActivity.OrderPhoto=0;
                                        finish();
                                        OrderCostActivity.startAction(mContext,car_order_no);
                                    }
                                });
                            }
                        });
                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        promptDialog.dismiss();
                    }
                });
                promptDialog.show();
                break;
            case R.id.tvIdInfoTakePhoto:
                witchPc=1;
                onePc=1;
                takePhoto();
                break;
            case R.id.tvIdInfoTakePhoto_two:
                witchPc=2;
                twoPc=1;
                takePhoto();
                break;
            case R.id.tvIdInfoTakePhoto_three:
                witchPc=3;
                threePc=1;
                takePhoto();
                break;
            case R.id.tvIdInfoTakePhoto_four:
                witchPc=4;
                fourPc=1;
                takePhoto();
                break;
            case R.id.ivIdInfo:
                if(onePc ==1){
                    ViewUtil.showBigPic(this,localPauth1);
                }
                break;
            case R.id.ivIdInfo_two:
                if( twoPc==1){
                    ViewUtil.showBigPic(this,localPauth2);
                }
                break;
            case R.id.ivIdInfo_three:
                if( threePc==1){
                    ViewUtil.showBigPic(this,localPauth3);
                }
                break;
            case R.id.ivIdInfo_four:
                if( fourPc==1){
                    ViewUtil.showBigPic(this,localPauth4);
                }
                break;
        }
    }
    /**
     * 开启拍照
     */
    private void takePhoto(){
        String ImageMessage="时间:"+ TimeUtil.getFormatTime()+"地点:"+""+"经纬度:";
        startActivityForResult(TakePhotoActivity.getTakePhotoStartIntent(mContext,
                DO_BIG_IMAGE, 1, 1, 1080,1080), UPLOAD_IMG);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 101 && resultCode == TakePhotoActivity.UPLOAD_SUCCESS) {
            if (data != null && data.getExtras() != null) {
//                Uri avatarUri = data.getExtras().getParcelable("PATH");
//                assert avatarUri != null;
               // loadEditImage(data.getExtras().getString("PATH"));
                currentUpdatePath=data.getExtras().getString("PATH");
//三种状态填充照片，三张照片都有显示下部按钮
                if(witchPc==1){
                    localPauth1 = currentUpdatePath;
                    MyApplication.getDefaultFinalBitmap(mContext).display(ivIdInfo, data.getExtras().getString("PATH"));
                    loadEditImage(data.getExtras().getString("PATH"));
                    Log.e("tag1","localPauth1 = "+localPauth1+",,,,,bmp = "+ localPauth1);
                    tvIdInfoTakePhoto.setText("点击重新拍摄");
                    onePc=1;
                }
                if(witchPc==2){
                    localPauth2 = currentUpdatePath;
                    MyApplication.getDefaultFinalBitmap(mContext).display(ivIdInfo_two, data.getExtras().getString("PATH"));
                    loadEditImage(data.getExtras().getString("PATH"));
                    tvIdInfoTakePhoto_two.setText("点击重新拍摄");
                    twoPc = 1;
                }
                if(witchPc==3){
                    localPauth3 = currentUpdatePath;
                    MyApplication.getDefaultFinalBitmap(mContext).display(ivIdInfo_three, data.getExtras().getString("PATH"));
                    loadEditImage(data.getExtras().getString("PATH"));
                    tvIdInfoTakePhoto_three.setText("点击重新拍摄");
                    threePc = 1;
                }
                if(witchPc==4){
                    localPauth4= currentUpdatePath;
                    MyApplication.getDefaultFinalBitmap(mContext).display(ivIdInfo_four, data.getExtras().getString("PATH"));
                    loadEditImage(data.getExtras().getString("PATH"));
                    tvIdInfoTakePhoto_four.setText("点击重新拍摄");
                    fourPc=1;
                }
                if(onePc==1&&twoPc==1&&threePc==1&&fourPc==1){
                    tvIdInfoNext.setClickable(true);
                    ViewUtil.GetViewShgape(tvIdInfoNext);
                    tvIdInfoNext.setBackgroundResource(R.drawable.bg_btn_green_selector);
                    ViewUtil.SetViewShgape(tvIdInfoNext);}
            }
        }
    }

    private void loadEditImage(String path) {
        new LHttpLib().editImage(mContext, memberId, Constants.IMAGE_TYPE_RETURN_ID, path, new BaseLHttpHandler(mContext, true, true) {
            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                final Image image = new Image().parse(jsonStatus.data.optJSONObject("image_info"));
                currentUpdatePath = image.source;
                if(witchPc==1){
                    Path1=image.source;
                }
                if(witchPc==2){
                    Path2=image.source;
                }
                if(witchPc==3){
                    Path3=image.source;
                }
                if(witchPc==4){
                    Path4=image.source;
                }
            }
        });
    }

    /**
     * 加载对应网点车位列表
     *//*
    private void loadEvent() {
        new LHttpLib().getParkingSpaceList(mContext, memberId, car_order_no, parking_site_no, new PageInfo(page, Constants.COUNT), new BaseLHttpHandler(mContext, true, true) {
            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                JSONArray jsonArray = jsonStatus.data.optJSONArray("parking_space_list");
                if (jsonArray != null && jsonArray.length() > 0) {
                    noParkLinear.setVisibility(View.GONE);
                    spaceView.setVisibility(View.VISIBLE);
                    parkSpaceList = new ArrayList<>();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        parkSpaceList.add(new ParkSpace().parse(jsonArray.optJSONObject(i)));
                    }
                    showView();
                }else {
                    noParkLinear.setVisibility(View.VISIBLE);
                    spaceView.setVisibility(View.GONE);
                }
            }
        });
    }*/

   /* *//**
     * 显示相应的视图
     *//*
    private void showView() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (parkSpaceList == null || parkSpaceList.size() <= 0) {
                    noParkLinear.setVisibility(View.VISIBLE);
                    viewPagePark.setVisibility(View.GONE);
                } else {
                    noParkLinear.setVisibility(View.GONE);
                    viewPagePark.setVisibility(View.VISIBLE);
                    //设置viewPage数据
                    //总的页数向上取整
                    final int totalPage = (int) Math.ceil(parkSpaceList.size() * 1.0 / 10);
                    for (int i = 0; i < totalPage; i++) {
                        //每个页面都是inflate出一个新实例
                        final View view = View.inflate(mContext, R.layout.item_park_space, null);
                        GridView  gridView = (GridView)view.findViewById(R.id.gridView);
                        final  ParkSpaceAdapter parkSpaceAdapter = new ParkSpaceAdapter(TakePhotoReturnCarActivity.this,R.layout.item_park_space_view);
                        parkSpaceAdapter.getDataList().clear();
                        if (totalPage == 1){
                            parkSpaceAdapter.getDataList().addAll(parkSpaceList);
                        }else {
                            parkSpaceAdapter.getDataList().addAll(parkSpaceList.subList(10*i,parkSpaceList.size()-10*i));
                        }
                        gridView.setAdapter(parkSpaceAdapter);
                        //添加item点击监听
                        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                                parkSpaceAdapter.getHolder().doOthers(parkSpaceAdapter.getItem(position),position);
                                parkSpaceAdapter.setCurrentPosition(position);
                                parkSpaceAdapter.notifyDataSetChanged();
                                currentSpaceNo = parkSpaceAdapter.getItem(position).parking_space_no;
                                if (!StringUtils.isBlank(currentUpdatePath)){
                                    tvIdInfoNext.setClickable(true);
                                    ViewUtil.GetViewShgape(tvIdInfoNext);
                                    tvIdInfoNext.setBackgroundResource(R.drawable.bg_btn_green_selector);
                                    ViewUtil.SetViewShgape(tvIdInfoNext);
                                }else {
                                    tvIdInfoNext.setClickable(false);
                                    ViewUtil.GetViewShgape(tvIdInfoNext);
                                    tvIdInfoNext.setBackgroundResource(R.drawable.bg_btn_gray);
                                    ViewUtil.SetViewShgape(tvIdInfoNext);
                                }
                            }
                        });
                        parkSpaceAdapter.notifyDataSetChanged();
                        //每一个GridView作为一个View对象添加到ViewPager集合中
                        views.add(view);
                    }
                    orderTotalAdapter.notifyDataSetChanged();
                    //添加小圆点指示器
                    //添加小圆点
                    ivPoints = new ImageView[totalPage];
                    for(int i = 0; i < totalPage; i++){
                        //循坏加入点点图片组
                        ivPoints[i] = new ImageView(mContext);
                        if(i==0){
                            ivPoints[i].setImageResource(R.mipmap.guide_dot_black);
                        }else {
                            ivPoints[i].setImageResource(R.mipmap.guide_dot_white);
                        }
                        ivPoints[i].setPadding(8, 8, 8, 8);
                        groupLinear.addView(ivPoints[i]);
                    }
                    //设置ViewPager的滑动监听，主要是设置点点的背景颜色的改变
                    viewPagePark.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
                        @Override
                        public void onPageSelected(int position) {
                            // TODO Auto-generated method stub
                            //currentPage = position;
                            for(int i=0 ; i < totalPage; i++){
                                if(i == position){
                                    ivPoints[i].setImageResource(R.mipmap.guide_dot_black);
                                }else {
                                    ivPoints[i].setImageResource(R.mipmap.guide_dot_white);
                                }
                            }
                        }
                    });
                }
            }
        });
    }*/
}
